﻿/***************************  Ccay IGrid CopyData  ***********************************/
(function(IGrid){
	var copyMsg = {
		// 确定要替换目标单元格内容？Are you sure you want to replace the contents of the selected cells?
		pasteReplace : "ccay.common.igrid.pasteReplace"
	};
	
	var coltype;//列类型
	
	var copyData = {
		/** @description 初始化事件
		 * 
		 */
	    
		initEvent: function (grid, op, cOp) {
			var gridId = grid.id, dataBody = grid.getDataBody(), 
				onCellClick = op.onCellClick, selectOp, hasFirstCell;
			var copyPanel = cOp.panel = $.create("textarea", "igrid-copydata-panel", null).appendTo(grid.getBodyPanel());
			copyPanel.bind({
				"blur": function () {
					copyPanel.data("noClear") || copyData.clearCopyCell(dataBody, grid);
				},
				"paste": function () {
					// 不能编辑的表格，不能进行粘贴
					if (op.editMode) {
						cOp.origData = copyPanel.val();
						setTimeout(function () {
							copyData.pasteDataEvent(copyPanel.val(), grid, op, cOp);
						}, 100);
					}
				}
			});
			// 设置shift键功能
			op.onCellClick = function (cellData, rowData, td, tr, colIdx, trIdx, col, grid, e) {
				var gridOp = grid.op, moverPos = td.position(), mover = grid.getDataBody(),
			        trigger = mover.find("td[field]:last");
			    //点击igrid左上角seq空白处选中所有表格  mwx301884
			    if (colIdx == 0 && isNaN(trIdx)) {
				   gridOp.selectAllPlace && copyData.setPlaceSign(e, moverPos, mover, trigger, null, grid);
			    }
				coltype = col.type;//单元格类型
				if (e.shiftKey && !col.inner) {
					//e.preventDefault();
					if (hasFirstCell) {
						copyData.setPlaceSign(e, td.position(), td, selectTarget, null, grid);
						hasFirstCell = selectTarget = null;
					} else {
						selectTarget = td;
						copyData.setSinglePlaceSign(selectTarget, grid);
						hasFirstCell = true;
					}
					Ccay.UI.setInputFocus(grid.op._copyOp.panel, null, true);
					return false;
				} else {
					hasFirstCell = selectTarget = null;
				}
				if (onCellClick) {
					return onCellClick.call(grid, cellData, rowData, td, tr, colIdx, trIdx, col, grid, e);
				}
			};
		},
		/** @description 初始化移动选中面板
		 * 
		 */
		initMover: function (grid, op) {
			var gridId = grid.id, bodyPanel = grid.getBodyPanel(), dataBody = grid.getDataBody(),
				scrollX = grid.getScroller(),
				_copyOp = op._copyOp, bodyHeight = bodyPanel.height(), bodyWidth = bodyPanel.width(),
				fixed, diffScroll, currPos, isDrag, isScrollX, scrollXInterval, scrollYInterval, currHeight, currWidth,
				// 创建mover拖拽层和创建选中数据的缓存区域
				moverPanel = $.create("div", "ccay-mover-vbox ccay-drop-activepanel", gridId + "_copydata_mover").hide().appendTo(bodyPanel);
			// 配置拖拽参数
    		op.dragObj = "custom";
    		op.dragOp = {
    			offset: false,
				virtual: false,
				container: bodyPanel,
    			target: ".grid-cell[field]", 
				handler: "#"+ gridId +"_copydata_mover",
				fnDrag: function(e, pos, mover, trigger, moverOp) {
					if (e.which == 1) {
						var oriScrollTop = bodyPanel.scrollTop(), oriScrollLeft = bodyPanel.scrollLeft();
						// 标示可以拖拽
						isDrag = true;
						_copyOp.trigger = trigger;
						// 将移动层定位到光标处
						pos.top += e.offsetY + oriScrollTop;
						pos.left += e.offsetX + oriScrollLeft;
						// 缓存光标定位单元格中的方位
						fixed = pos;
						fixed.offsetY = e.offsetY;
						fixed.offsetX = e.offsetX;
						fixed.oriScrollTop = oriScrollTop;
						fixed.oriScrollLeft = oriScrollLeft;
						// 缓存滚动差值
						diffScroll = {top: 0, left: 0}
						// 计算光标左右、上下的间距
						copyData.getPanelSpanByCursor(pos, dataBody, op, grid);
						bodyPanel.on("scroll.copy", function (e) {
							if (isScrollX !== true) {
								diffScroll.top = $(this).scrollTop() - fixed.oriScrollTop;
								copyData.setMover(mover, currPos, fixed, op._bodySpan, diffScroll);
							}
						});
					} else {
						return false;
					}
				},
				fnMove: function (e, pos, mover, trigger, moverOp) {
					if (isDrag) {
						// 缓存当前光标定位
						currPos = pos;
						copyData.setMover(mover, currPos, fixed, op._bodySpan, diffScroll);
						currHeight = pos.top - fixed.oriScrollTop;
						currWidth = pos.left - fixed.oriScrollLeft;
						// 触发竖向滚动
						if (currHeight > bodyHeight || currHeight < 0) {
							scrollYInterval || (scrollYInterval = setInterval(function () {
								bodyPanel.scrollTop(bodyPanel.scrollTop() + (currHeight < 0 ? currHeight :  currHeight - bodyHeight));
							}, 10));
						} else {
							scrollYInterval && clearInterval(scrollYInterval);
							scrollYInterval = null;
						}
						// 触发横向滚动
						if (currWidth > bodyWidth || currWidth < 0) {
							scrollXInterval || (scrollXInterval = setInterval(function () {
								scrollX.scrollLeft(scrollX.scrollLeft() + (currWidth < 0 ? currWidth :  currWidth - bodyWidth));
							}, 10));
						} else {
							scrollXInterval && clearInterval(scrollXInterval);
							scrollXInterval = null;
						}
					}
					return false;
				},
				fnBeginDrop: function (e, pos, mover, trigger, moverOp) {
					if (isDrag) {
						scrollXInterval && clearInterval(scrollXInterval);
						scrollYInterval && clearInterval(scrollYInterval);
						isDrag = fixed = diffScroll = currPos = isScrollX = scrollXInterval = scrollYInterval = currHeight = null;
						// 设置缓存的复制粘贴面板的位置
						op._copyOp.panel.css({top: e.clientY, left: e.clientX});
						// 设置当前grid为只读状态
						op._readonly = true;
						// 清除所有的编辑器
						(op._activeEditTr != null) && grid.clearActiveEditor();
						// 设置选中的单元格标示
						copyData.setPlaceSign(e, pos, mover, trigger, moverOp, this);
						bodyPanel.off("scroll.copy");
						mover.hide();
					}
					return false;
				}
			};
			op.handler.scrollX.add(function(grid,endIndex,scrollX) {
					if (isDrag) {
						isScrollX = true;
						var bodySpan = op._bodySpan, trigger = _copyOp.trigger;
						fixed.left = (trigger.width() > 5) ? trigger.position().left + fixed.offsetX : (op._fzColSet && op._fzColSet.count ? op._fzColSet.width + 20 : 0);
						diffScroll.left = bodyPanel.scrollLeft() - fixed.oriScrollLeft;
						copyData.getPanelSpanByCursor({top: bodySpan.topHeight, left: fixed.left}, dataBody, op, grid);
						copyData.setMover(moverPanel, currPos, fixed, op._bodySpan, diffScroll);
					}
            });
		},
		/** @description 定位光标开始拖拽时，在距离数据面板的上，下，左，右的边距
		 * 
		 */
		getPanelSpanByCursor: function (pos, panel, op, grid) {
			op._bodySpan = {
				leftWidth: pos.left,
				rightWidth: panel.outerWidth() - pos.left,
				topHeight: pos.top,
				bottomHeight: panel.outerHeight() - pos.top
			};
		},
		/** @description 设置移动选中面板
		 * 
		 */
		setMover: function (mover, pos, fixed, bodySpan, diffScroll) {
			var realTop = pos.top + diffScroll.top,
				realLeft = pos.left +  + diffScroll.left,
				mTop = (realTop < 0 ) ? 0 : Math.min(realTop, fixed.top),
				mLeft = (realLeft < 0) ? 0 : Math.min(realLeft, fixed.left),
				mHeight, mWidth;
			// 光标在定位是上方
			if (realTop < fixed.top) {
				mHeight = bodySpan.topHeight - mTop;
			} else {
				// 光标在定位是下方
				mHeight = Math.min(realTop - mTop, bodySpan.bottomHeight);
			}
			// 光标在定位是左方
			if (realLeft < fixed.left) {
				mWidth = bodySpan.leftWidth - mLeft;
			} else {
				// 光标在定位是右方
				mWidth = Math.min(realLeft - mLeft, bodySpan.rightWidth);
			}
			mover.css({top: mTop, left: mLeft, height: mHeight, width: mWidth});
		},
		/** @description 设置单个单元格选中
		 * 
		 */
		setSinglePlaceSign: function (currCell, grid) {
			var gridOp = grid.op, _copyOp = gridOp._copyOp, copyOp = gridOp.copyOp, 
				dataHelper = Ccay.DataHelper, currRow, tdHtml, cellField = currCell.attr("field");
			if (cellField) {
				// 设置选中单元格样式
				currCell.addClass("igrid-cell-copy");
				// 获取当前行
				currRow = grid.getCellRow(currCell);
				// 设置选中数据的格式
				if (copyOp.renderData) {
					tdHtml = copyOp.fnToRenderData ? copyOp.fnToRenderData.call(grid, currCell) : currCell.text();
				} else {
					tdHtml = dataHelper.getObj(grid.getValueField(cellField), grid.getRowData(currRow));
				}
				_copyOp.colCount = _copyOp.rowCount = 1;
				_copyOp.panel.val(tdHtml);
				Ccay.UI.setInputFocus(_copyOp.panel, null, true);
			}
		},
		/** @description 设置拖拽选中
		 * 
		 */
		setPlaceSign: function (e, moverPos, mover, trigger, moverOp, grid) {
			var gridOp = grid.op, _copyOp = gridOp._copyOp, copyOp = gridOp.copyOp, dataHelper = Ccay.DataHelper,
				moverX = moverPos.left, moverY = moverPos.top, currCell = trigger, cellPos = trigger.position(),
				startNo = grid.getColumnIndex(currCell), tempTdList, tempTrList = [],
				tdHtml, trHtml, cellField, nextCell, currRow, nextRow, rowPos, isCellNext, isRowNext;
			// 当移动面板在单元格右方时，往下一个单元格执行，否则往上一个单元格执行
			if (cellPos.left < moverPos.left) {
				moverX += mover.width();
				isCellNext = true;
			}
			// 当移动面板在单元格下方时，往下一个行执行，否则往上一行执行
			if (cellPos.top < moverPos.top) {
				moverY += mover.height();
				isRowNext = true;
			}
			// 从定位单元格开始，往下行循环
			do {
				tempTdList = [];
				// 在该行中，从定位单元格开始，往后循环
				do {
					cellField = currCell.attr("field");
					if (cellField) {
						// 设置选中单元格样式
						currCell.addClass("igrid-cell-copy");
						// 获取当前行
						currRow = grid.getCellRow(currCell);
						// 设置选中数据的格式
						if (copyOp.renderData) {
							tdHtml = copyOp.fnToRenderData ? copyOp.fnToRenderData.call(grid, currCell) : currCell.text();
						} else {
							tdHtml = dataHelper.getObj(grid.getValueField(cellField), grid.getRowData(currRow));
						}
					}
					// 往后执行
					if (isCellNext) {
						tempTdList.push(tdHtml);
						nextCell = currCell.next();
					} else {
						if (cellField) {
							tempTdList.unshift(tdHtml);
						}
						// 往前执行
						nextCell = currCell.prev();
					}
					if (nextCell.length) {
						currCell = nextCell;
						cellPos = currCell.position();
					} else {
						break;
					}
				} while (isCellNext ? (cellPos.left < moverX) : (moverX < cellPos.left + currCell.width()));
				_copyOp.colCount = tempTdList.length;
				trHtml = tempTdList.join("\t");
				// 往后执行
				if (isRowNext) {
					tempTrList.push(trHtml);
					nextRow = currRow.next();
				} else {
					// 往前执行
					tempTrList.unshift(trHtml);
					nextRow = currRow.prev();
				}
				if (nextRow.length) {
					 rowPos = nextRow.position();
					 currCell = grid.getRowCell(nextRow, startNo);
				} else {
					break;
				}
			} while (isRowNext ? (rowPos.top < moverY) : (moverY < rowPos.top + nextRow.height()));
			gridOp.allowMoveCopy && copyData.moveCopyData(grid);
			_copyOp.rowCount = tempTrList.length;
			_copyOp.panel.val(tempTrList.join("\n"));
			Ccay.UI.setInputFocus(_copyOp.panel, null, true);
		},
		/** @description 设置粘贴的选中区域
		 * 
		 */
		setSelectPanel: function (targetCell, rowCount, colCount, grid) {
			var cellPos = targetCell.position(), targetRow = grid.getCellRow(targetCell),
				bodyPanel = grid.getBodyPanel(),
				colIdx = 0, rowIdx = 0, width = 0, height = 0;
			// 计算宽度
			for(; colIdx < colCount; colIdx++) {
				width += targetCell.outerWidth();
				targetCell = targetCell.next();
			}
			// 计算高度
			for (; rowIdx < rowCount; rowIdx++) {
				height += targetRow.outerHeight();
				targetRow = targetRow.next();
			}
			$("#" + grid.id + "_copydata_mover").css({
				top: cellPos.top + bodyPanel.scrollTop(),
				left: cellPos.left + bodyPanel.scrollLeft(),
				width: width,
				height: height
			}).show();
		},
		/** @description 粘贴数据事件
		 * 
		 */
		pasteDataEvent: function (copyDatas, grid, op, cOp) {
			dataHelper = Ccay.DataHelper;
			if (copyDatas) {
				var targetCell = grid.getDataBody().find(".igrid-cell-copy"),
					dataLen = copyDatas.length - 1, trDatas, rowCount, colCount;
				// 去除字符串结尾的换行符
				if (copyDatas.substr(dataLen) == "\n") {
					copyDatas = copyDatas.substring(0, dataLen);
				}
				// 支持lookup粘贴---mwx301884 2015-11-12----
				var islookup = grid.getDataBody().find(".enum.igrid-cell-copy");
				// 粘贴时判断数据是否存在
				if (islookup.length != 0 && copyDatas.indexOf("\n") == -1) {
					var isexit = false;
					var lookuplist = [];
					//获取所有lookup下拉框数据
					lookuplist = $("ul[class='list'] li");
					// var obj = {};
					if (lookuplist) {
						for (var i = 0; i < lookuplist.length; i++) {
							// obj[$(lookuplist[i]).attr("val")] =
							// $(lookuplist[i]).text();
							if (copyDatas == $(lookuplist[i]).text()
									|| copyDatas == $(lookuplist[i])
											.attr("val")) {
								copyDatas = $(lookuplist[i]).attr("val");
								coltype = "";
								isexit = true;
								break;
							}

						}
						if (!isexit) {
							copyDatas = "";
						}
					}
				}
				// 单元格为integer类型需要转成字符串
				if (coltype == "integer") {
					var numval = $(grid.getFocusRow()).find(".num");
					var Datas = dataHelper.getObj(grid.getValueField(numval
							.attr("field")), grid.getRowData(numval));
					copyDatas = Datas.toString();
					coltype = "";
				}
				//统计跨几列
				var arr = [];
				for(var i =0;i < targetCell.length;i++){
					arr[grid.getColumnOp($(targetCell[i])).field] = grid.getColumnOp($(targetCell[i])).field;
				};
				//如果一个单元格跨列复制粘贴
                if (Object.keys(arr).length > 1 && copyDatas.indexOf("\t") == -1) {
                	var temp = [];
                	for (var s = 0; s < Object.keys(arr).length; s++) {
                		temp.push(copyDatas);
					}
                	copyDatas = temp.toString().replace(",","\t");
				}
				// 获取剪贴板数据
				trDatas = copyDatas.split("\n");
				// 复制一行数据粘贴到多行数据
				if (copyDatas.indexOf("\n") == -1) {
					for (var i = 0; i < cOp.rowCount - 1; i++) {
						trDatas.push(copyDatas);
					}
				}  
				rowCount = Math.min(trDatas.length, op.copyOp.maxLine);
				colCount = trDatas[0].split("\t").length;
				if (cOp.rowCount < rowCount || cOp.colCount < colCount) {
					cOp.panel.data("noClear", true);
					// 设置选中区域
					copyData.setSelectPanel(targetCell, rowCount, colCount, grid);
					// 提示是否替换内容
					Ccay.UI.confirm(copyMsg.pasteReplace, function (result) {
						cOp.panel.data("noClear", null).focus();
						$("#" + grid.id + "_copydata_mover").hide();
						if (result) {
							copyData.pasteData(trDatas, rowCount, targetCell, grid);
						} else {
							cOp.panel.val(cOp.origData);
							Ccay.UI.setInputFocus(cOp.panel, null, true);
						}
					}, true);
				} else {
				 	copyData.pasteData(trDatas, rowCount, targetCell, grid);
				}
			}
		},
		/** @description 粘贴数据
		 * 
		 */
		pasteData: function (trDatas, rowCount, targetCell, grid) {
			var gridOp = grid.op, copyOp = gridOp.copyOp, startNo = grid.getColumnIndex(targetCell), 
				dataHelper = Ccay.DataHelper, tempCell, targetCellOp, tdDatas, oldTdData;
			// 循环每一行
			$.each(trDatas, function (trIdx, trData) {
				if (targetCell.length && trIdx < rowCount) {
					tdDatas = trData.split("\t");
					// 循环修改单元格
					$.each(tdDatas, function (tdIdx, tdData) {
						if (targetCell.length) {
							copyOp.fnToOriginalData && (tdData = copyOp.fnToOriginalData.call(grid, targetCell, tdData));
							// 获取原始数据
							oldTdData = copyOp.renderData ? targetCell.text() : dataHelper.getObj(grid.getValueField(targetCell.attr("field")), grid.getRowData(targetCell));
							var tmpCell = $(targetCell[0]);
						    var cellOp = grid.getColumnOp(tmpCell);
						    //判断当前列是否可编辑，如果有则粘贴
							if(cellOp.editable !== false){
							   if(!grid.op.fnBeforeEdit || grid.op.fnBeforeEdit && grid.op.fnBeforeEdit.call(grid,grid.cellValue(tmpCell,cellOp),tmpCell,cellOp,grid.getRowData(tmpCell))!==false){
							   	  // 修改单元格数据并验证
							   	  grid.updateCellData(tmpCell, tdData, oldTdData);
							   }
							}
							tempCell = targetCell;
							targetCell = targetCell.next();
						} else {
							targetCell = tempCell;
							return false;
						}
					});
					// 获取下一行的开始单元格
					targetCell = grid.getRowCell(grid.getCellRow(targetCell.length ? targetCell : tempCell).next(), startNo);
				} else {
					return false;
				}
			});
			grid.getDataBody().attr("tabindex", -1).focus();
			gridOp.showStatus && grid.updateStatus();
		},
		/** @description 清除单元格选中状态
		 * 
		 */
		clearCopyCell: function (dataBody, grid) {
			// 清除当前grid的只读状态
			grid.op._readonly = null;
			dataBody.removeAttr("tabindex").find(".igrid-cell-copy").removeClass("igrid-cell-copy");
			grid.op.allowMoveCopy && copyData.clearMoveCopyData(grid);
		},
		/**
		 * @description 拖拽复制数据
		 */
		moveCopyData: function(grid){
			var dataBody = grid.getDataBody();
			//选中一个单元格才允许拖拽复制数据，否则退出复制
			if(dataBody.find(".igrid-cell-copy").length !== 1){
				return;
			}
			var bodyPanel = grid.getBodyPanel(),
				bodyHeight = bodyPanel.height(),
				bodyScrollTop = bodyPanel.scrollTop(),
				offsetTop = bodyPanel.offset().top,
				maxHeight = grid.getBodyPlace().height(),//最大高度
				currCell = dataBody.find(".igrid-cell-copy:first"),//当前选中的单元格
				curPos = currCell.position(),//当前单元格的位置
				curTop = curPos.top,//当前单元格的top
				curLeft = curPos.left,//当前单元格的left
				curWidth = currCell.outerWidth(),//当前单元格的宽度
				curHeight = currCell.outerHeight(),//当前单元格的高度
				scrollYInterval = null,
				range = null,//所选区域。1、大于0为下部分，2、小于0为上部分。
				currLastRow = null,//记录最后一次复制值的行
				currLastRange = null;//记录最后一次所选中的区域。1、大于0为下部分，2、小于0为上部分。
			/** 创建组件选择区域和拖拽组件 begin **/
			var box = $('<div class="igrid-copy-panel"></div>');//创建选中区域
			var mover = $('<div class="igrid-copy-box"></div>');//创建拖拽
			var cpPos = {
				width:curWidth,
				height:curHeight-1,
				top:Math.min(Math.max(0,curTop+bodyScrollTop),maxHeight-4)-1,
				left:curLeft-2
			};
			var fixed = {
				top:curTop+curHeight+bodyScrollTop-4,
				left:curLeft+curWidth-4
			};
			box.css(cpPos).appendTo(bodyPanel);
			mover.css(fixed).appendTo(bodyPanel);
			/** 创建组件选择区域和拖拽组件 end **/
			
			/**  绑定事件 begin **/
			mover.mousedown(function(){
				//锁定复制数据，不允许清除。
				bodyPanel.children(".igrid-copydata-panel").data("noClear",true);
			}).drag({
				offset: false,
				virtual: false,
				container: bodyPanel,
				onDragStart:function(e,pos,mover,trigger){
					fixed.startTop = bodyPanel.scrollTop();
					pos.top = pos.top + bodyPanel.scrollTop();
					fixed.scrollTop === undefined && (fixed.scrollTop = bodyPanel.scrollTop());
					$(document.body).css("cursor","move");
				},
				onMoving: function (e, pos) {
					var scrollTop = Math.min(Math.max(0,pos.top-fixed.startTop+bodyPanel.scrollTop()),maxHeight-4);
					range = scrollTop+2-curTop-fixed.scrollTop;
					mover.css({
						top:scrollTop
					});
					box.css({
						top:(range>0 ? cpPos.top : cpPos.top+range),
						height:(range>0 ? Math.max(cpPos.height,range) : cpPos.height-range)
					});
					if(e.pageY > offsetTop+bodyHeight || e.pageY < offsetTop){
						scrollYInterval || (scrollYInterval = setInterval(function () {
							bodyPanel.scrollTop(bodyPanel.scrollTop() + (e.pageY < offsetTop ? -24 : 24));
						},10));
					}else{
						scrollYInterval && clearInterval(scrollYInterval);
						scrollYInterval = null;
					}
					return false;
				},
				onDroped:function(){
					scrollYInterval && clearInterval(scrollYInterval);
					scrollYInterval = null;
					$(document.body).css("cursor","");
					var cell = currCell,
						currRow = cell.parent()[0],
						col = grid.getColumnOp(cell),
						cellValue = grid.cellValue(cell,col),//获取需要复制的单元格的值
						cellEmptyValue = cellValue instanceof Object ? {value:"",text:""} : "";//清空的值
						nextRow = null,
						selector = "td[_col='{0}']:first".formatValue(currCell.attr("_col"));//需要复制值的单元格的选择器
					/** 清空移除部分的值 begin **/
					if(currLastRow && currLastRange !== null){
						nextRow = currLastRow;
						if(currLastRange > 0 && range < 0){
							//清空上次下面区域所有选中的单元格的值
							while(nextRow[0] != currRow){
								var nextCell = nextRow.children(selector),nextCol = grid.getColumnOp(nextCell);
								grid.cellValue(nextCell,nextCol,cellEmptyValue,true);
								nextRow = nextRow.prev();
							}
						}else if(currLastRange > 0 && range > 0){
							//清空上次下面区域部分移除的单元格的值
							var boxCurHeight = cell.position().top + box.height();
							while(nextRow[0] != currRow && boxCurHeight <= nextRow.position().top){
								var nextCell = nextRow.children(selector),nextCol = grid.getColumnOp(nextCell);
								grid.cellValue(nextCell,nextCol,cellEmptyValue,true);
								nextRow = nextRow.prev();
							}
						}else if(currLastRange < 0 && range < 0){
							//移除上次上面区域部分移除的单元格的值
							var boxTop = box.position().top;
							while(nextRow[0] != currRow && boxTop >= nextRow.position().top+nextRow.height()){
								var nextCell = nextRow.children(selector),nextCol = grid.getColumnOp(nextCell);
								grid.cellValue(nextCell,nextCol,cellEmptyValue,true);
								nextRow = nextRow.next();
							}
						}else if(currLastRange < 0 && range > 0){
							//移除上次上面区域所以选中的单元格的值
							while(nextRow[0] != currRow){
								var nextCell = nextRow.children(selector),nextCol = grid.getColumnOp(nextCell);
								grid.cellValue(nextCell,nextCol,cellEmptyValue,true);
								nextRow = nextRow.next();
							}
						}
					}
					/** 清空移除部分的值 end **/
					currLastRow = null;
					currLastRange = range;
					/** 给选中部分的复制值 begin **/
					if(range > 0){
						//向下复制
						var boxCurHeight = cell.position().top + box.height();
						nextRow = cell.parent().next();
						while(nextRow.length && boxCurHeight > nextRow.position().top){
							var nextCell = nextRow.children(selector),nextCol = grid.getColumnOp(nextCell);
							//如果当前列可编辑则 进行粘贴，否则跳过
							if(nextCol.editable!==false){
							   //如果 当前配置中不存在fnBeforeEdit 或者  存在fnBeforeEdit并且不返回false 
							   if(!grid.op.fnBeforeEdit || grid.op.fnBeforeEdit && grid.op.fnBeforeEdit.call(grid,grid.cellValue(nextCell,nextCol),nextCell,nextCol,grid.getRowData(nextCell))!==false){
									if(grid.cellValue(nextCell,nextCol,cellValue,true)!==false){
									   if (!grid.isNewRow(nextRow)) {
									        nextCell.addClass('cell-update');
									        nextCell.hasClass(Ccay.UI.IGrid.Const.updated.css) || nextCell.addClass(Ccay.UI.IGrid.Const.updated.css);
									     }
									}
							   }
							}
							currLastRow = nextRow;
							nextRow = nextRow.next();
						}
					}else if(range < 0){
						//向上复制
						var boxTop = box.position().top;
						nextRow = cell.parent().prev();
						while(nextRow.length && boxTop < nextRow.position().top+nextRow.height()){
							var nextCell = nextRow.children(selector),nextCol = grid.getColumnOp(nextCell);
							if(grid.cellValue(nextCell,nextCol,cellValue,true)!==false){
								   if (!grid.isNewRow(nextRow)) {
								        nextCell.addClass('cell-update');
								        nextCell.hasClass(Ccay.UI.IGrid.Const.updated.css) || nextCell.addClass(Ccay.UI.IGrid.Const.updated.css);
								     }
								}
							currLastRow = nextRow;
							nextRow = nextRow.prev();
						}
					}
					/** 给选中部分的复制值 end **/
				}
			});
			$(document).bind("mouseup.igrid-copy", function(){
				//移除复制数据的锁定，允许清除。
				bodyPanel.children(".igrid-copydata-panel").removeData("noClear").focus();
			});
			/**  绑定事件 end **/
		},
		/**
		 * @description 清除拖拽复制的状态
		 */
		clearMoveCopyData:function(grid){
			$(document).unbind("mouseup.igrid-copy");
			grid.getBodyPanel().children("div.igrid-copy-box,div.igrid-copy-panel").remove();
		}
	};
	
	Ccay.UI.XControl.addPlugin("XGrid", "copyData", {
	    build: function (grid, op) {
	    	if(op.allowCopy){
	    		var copyOp = {
	    			maxLine: 100,
	    			renderData: false,
	    			fnToRenderData: null,
	    			fnToOriginalData: null
	    		};
	    		// 显示错误提示消息
	    		op.showTip || (op.showTip = true);
	    		op.copyOp = op.copyOp ? $.extend(copyOp, op.copyOp) : copyOp;
	    		op._copyOp = {};
	    		// 初始化事件
	    		copyData.initEvent(grid, op, op._copyOp);
	    		// 初始化移动面板
	    		copyData.initMover(grid, op);
	    	}
	    }
	});
})(Ccay.UI.IGrid);